package ldh.fx.adminfx.controller.sub;

import javafx.beans.InvalidationListener;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import ldh.fx.adminfx.pojo.Product;
import ldh.fx.adminfx.util.RegionUtil;
import ldh.fx.adminfx.util.StringUtil;
import org.kordamp.ikonli.fontawesome.FontAwesome;
import org.kordamp.ikonli.javafx.FontIcon;

import java.net.URL;
import java.util.ResourceBundle;

public class ProductItemController implements Initializable {

    private Product product;

    @FXML private Pane root;
    @FXML private ImageView productImage;
    @FXML private Label isNew;
    @FXML private TextArea productNameTextArea;
    @FXML private Hyperlink productCategoryHyperlink;
    @FXML private Pane productInfoPane;
    @FXML private TextField discountPriceTextField;
    @FXML private TextField priceTextField;
    @FXML private Pane starBox;
    @FXML private Label productExistLabel;


    public void setProduct(Product product) {
        this.product = product;
        setData();
    }

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
//        setData();
//        productNameTextArea.setFont(Font.font(18));
//        productNameTextArea.setWrappingWidth(400);
        root.widthProperty().addListener((InvalidationListener) l->{
            Font font = Font.font(18);
            double h = RegionUtil.calcStringHeight(productNameTextArea.getText(), 350, font);
//            System.out.println("H:" + h);
            productNameTextArea.setPrefHeight(h+12);
        });
    }

    private void setData() {
        if (product == null) return;
        if (StringUtil.isNotNull(product.getImg())) {
            productImage.setImage(new Image(product.getImg()));
        }
        if(product.getNew()) {
            isNew.setVisible(true);
        }
        productNameTextArea.setText(StringUtil.getDefaultValue(product.getName(), "").toString());
        productCategoryHyperlink.setText(StringUtil.getDefaultValue(product.getCategory(), "").toString());

        if (product.getInfoes() != null) {
            productInfoPane.getChildren().clear();
            for(String info : product.getInfoes()) {
                Label infoLabel = new Label(info);
                FontIcon fontIcon = FontIcon.of(FontAwesome.findByDescription(FontAwesome.DOT_CIRCLE_O.getDescription()));
                infoLabel.setGraphic(fontIcon);
                productInfoPane.getChildren().add(infoLabel);
            }
        }

        discountPriceTextField.setText("$" + StringUtil.getDefaultValue(product.getDiscountPrize(), "0.00").toString());
        priceTextField.setText("$" + StringUtil.getDefaultValue(product.getPrize(), "0.00").toString());

        int t = product.getProgress().intValue();
        starBox.getChildren().clear();
        for(int i=0; i<5; i++) {
            if (i < product.getProgress()) {
                if (i == t && t + 1 > product.getProgress().intValue()) {
                    FontIcon fontIcon2 = FontIcon.of(FontAwesome.findByDescription(FontAwesome.STAR_HALF_O.getDescription()));
                    fontIcon2.getStyleClass().add("star-active");
                    starBox.getChildren().add(fontIcon2);
                } else {
                    FontIcon fontIcon = FontIcon.of(FontAwesome.findByDescription(FontAwesome.STAR.getDescription()));
                    fontIcon.getStyleClass().add("star-active");
                    starBox.getChildren().add(fontIcon);
                }
            } else {
                FontIcon fontIcon = FontIcon.of(FontAwesome.findByDescription(FontAwesome.STAR.getDescription()));
                fontIcon.getStyleClass().add("star-default");
                starBox.getChildren().add(fontIcon);
            }
        }


        productExistLabel.setText(product.getEnough() ? "足够" : "不足");
    }
}
